<?php
include 'config.php';



function getMainDevices($conn) {

    echo "<button class='btn btn-primary' id='addMainDevice'>新增主设备</button>";

    $sql = "SELECT * FROM Main";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        echo "<table class='table table-striped'>";
        echo "<thead><tr><th>ID</th><th>编号</th><th>位置</th><th>管理人</th><th>操作</th></tr></thead>";
        echo "<tbody>";
        while ($row = $result->fetch_assoc()) {
            echo "<tr><td>" . $row['ID'] . "</td><td>" . $row['编号'] . "</td><td>" . $row['位置'] . "</td><td>" . $row['管理人'] . "</td>";
            echo "<td><button class='btn btn-primary view-sub-devices' data-mainid='" . $row['ID'] . "'>查看附件</button>";
             echo " <button class='btn btn-warning update-main-device' data-mainid='" . $row['ID'] . "'>变更</button></td></tr>";
        }
        echo "</tbody>";
        echo "</table>";
    } else {
        echo "暂无主设备数据。";
    }
}


function getSubDevices($conn) {

    $mainId = isset($_GET['main_id']) ? $_GET['main_id'] : '';

    if ($mainId !== '') {
        $sql = "SELECT * FROM Sub WHERE mainId = '$mainId'";
    } else {
        $sql = "SELECT * FROM Sub";
    }


    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        echo "<table class='table table-striped'>";
        echo "<thead><tr><th>ID</th><th>名称</th><th>编号</th><th>类别</th><th>型号规格</th><th>数量</th><th>有效期</th><th>计量单位</th></tr></thead>";
        echo "<tbody>";
        while ($row = $result->fetch_assoc()) {
            echo "<tr><td>" .$row['ID'] . "</td><td>" . $row['名称'] . "</td><td>" . $row['编号'] . "</td><td>" . $row['类别'] . "</td><td>" . $row['型号规格'] . "</td><td>" . $row['数量'] . "</td><td>" . $row['有效期'] . "</td><td>" . $row['计量单位'] . "</td></tr>";
        }
        echo "</tbody>";
        echo "</table>";
    } else {
        echo "暂无附件数据。";
    }
}


function getMainDeviceForm($conn) {
    $mainId = isset($_GET['main_id']) ? $_GET['main_id'] : '';

    if ($mainId !== '') {
        $sql = "SELECT * FROM Main WHERE ID='$mainId'";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            $row = $result->fetch_assoc();
            echo "<h3>编辑主设备</h3>";
            echo "<form id='update-main-device-form'>";
            echo "<input type='hidden' name='action' value='update_main_device'>";
            echo "<input type='hidden' name='ID' value='" . $row['ID'] . "'>";
            echo "<div class='form-group'><label>编号：</label><input type='text' class='form-control' name='编号' value='" . $row['编号'] . "'></div>";
            echo "<div class='form-group'><label>位置：</label><input type='text' class='form-control' name='位置' value='" . $row['位置'] . "'></div>";
            echo "<div class='form-group'><label>管理人：</label><input type='text' class='form-control' name='管理人' value='" . $row['管理人'] . "'></div>";
            echo "</form>";
        } else {
            echo "找不到指定的主设备。";
        }
    } else {
        echo "缺少主设备ID。";
    }
}

function updateMainDevice($conn) {
    $ID = isset($_POST['ID']) ? $_POST['ID'] : '';
    $编号 = isset($_POST['编号']) ? $_POST['编号'] : '';
    $位置 = isset($_POST['位置']) ? $_POST['位置'] : '';
    $管理人 = isset($_POST['管理人']) ? $_POST['管理人'] : '';

    if ($ID !== '' && $编号 !== '' && $位置 !== '' && $管理人 !== '') 
    {
        $sql = "UPDATE Main SET 编号='$编号', 位置='$位置', 管理人='$管理人' WHERE ID='$ID'";
        if ($conn->query($sql) === TRUE) {
            echo 'success';
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    } else {
        echo 'Please fill out all fields.';
    }
}


function getAddMainDeviceForm() {
?>
  <form id="add-main-device-form">
    <input type="hidden" name="action" value="add_main_device">
    <div class="form-group">
      <label>编号：</label>
      <input type="text" class="form-control" name="编号" required>
    </div>
    <div class="form-group">
      <label>位置：</label>
      <input type="text" class="form-control" name="位置" required>
    </div>
    <div class="form-group">
      <label>管理人：</label>
      <input type="text" class="form-control" name="管理人" required>
    </div>
  </form>
<?php
}





function addMainDevice($conn) {
    $编号 = isset($_POST['编号']) ? $_POST['编号'] : '';
    $位置 = isset($_POST['位置']) ? $_POST['位置'] : '';
    $管理人 = isset($_POST['管理人']) ? $_POST['管理人'] : '';

    if ($编号 !== '' && $位置 !== '' && $管理人 !== '') {
        $sql = "INSERT INTO Main (编号, 位置, 管理人) VALUES ('$编号', '$位置', '$管理人')";
        if ($conn->query($sql) === TRUE) {
            echo 'success';
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    } else {
        echo 'Please fill out all fields.';
    }
}

$action = isset($_GET['action']) ? $_GET['action'] : $_POST['action'] ;

switch ($action) {
    case 'get_main_devices':
        getMainDevices($conn);
        break;
    case 'get_sub_devices':
        getSubDevices($conn);
        break;
    case 'get_main_device_form':
        getMainDeviceForm($conn);
        break;
    case 'update_main_device':
        updateMainDevice($conn);
        break;
    case 'get_add_main_device_form':
        getAddMainDeviceForm();
        break;
    case 'add_main_device':
        addMainDevice($conn);
        break;        
    //...其他操作
}

$conn->close();


?>